package com.vitaf.cash.app.databinding

import android.annotation.SuppressLint
import android.widget.ImageView
import androidx.annotation.DrawableRes
import androidx.databinding.BindingAdapter
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions

object GlideBindingAdapters {

    // 基础图片加载
    @BindingAdapter("bindImageUrl")
    @JvmStatic
    fun loadImage(view: ImageView, url: String?) {
        loadImageWithPlaceholder(view, url, 0)
    }

    // 带占位图的图片加载
    @SuppressLint("CheckResult")
    @BindingAdapter("bindImageUrl", "bindPlaceholderRes")
    @JvmStatic
    fun loadImageWithPlaceholder(view: ImageView, url: String?, @DrawableRes placeholderRes: Int) {
        if (!url.isNullOrEmpty()) {
            val request = Glide.with(view.context).load(url)
            if (placeholderRes != 0) {
                request.placeholder(placeholderRes).error(placeholderRes)
            }
            request.into(view)
        } else if (placeholderRes != 0) {
            view.setImageResource(placeholderRes)
        } else {
            view.setImageDrawable(null)
        }
    }

    // 圆形图片
    @BindingAdapter("loadCircleImage")
    @JvmStatic
    fun loadCircleImage(view: ImageView, url: String?) {
        loadCircleImageWithPlaceholder(view, url, 0)
    }

    @SuppressLint("CheckResult")
    @BindingAdapter("loadCircleImage", "bindPlaceholderRes")
    @JvmStatic
    fun loadCircleImageWithPlaceholder(view: ImageView, url: String?, @DrawableRes placeholderRes: Int) {
        if (!url.isNullOrEmpty()) {
            val request = Glide.with(view.context)
                .load(url)
                .apply(RequestOptions.bitmapTransform(CircleCrop()))

            if (placeholderRes != 0) {
                request.placeholder(placeholderRes).error(placeholderRes)
            }
            request.into(view)
        } else if (placeholderRes != 0) {
            view.setImageResource(placeholderRes)
        } else {
            view.setImageDrawable(null)
        }
    }

    // 圆角图片
    @BindingAdapter("loadRoundedImage", "cornerRadius")
    @JvmStatic
    fun loadRoundedImage(view: ImageView, url: String?, cornerRadius: Int) {
        loadRoundedImageWithPlaceholder(view, url, cornerRadius, 0)
    }

    @SuppressLint("CheckResult")
    @BindingAdapter("loadRoundedImage", "cornerRadius", "bindPlaceholderRes")
    @JvmStatic
    fun loadRoundedImageWithPlaceholder(
        view: ImageView,
        url: String?,
        cornerRadius: Int,
        @DrawableRes placeholderRes: Int
    ) {
        if (!url.isNullOrEmpty()) {
            val request = Glide.with(view.context)
                .load(url)
                .apply(RequestOptions.bitmapTransform(RoundedCorners(cornerRadius)))

            if (placeholderRes != 0) {
                request.placeholder(placeholderRes).error(placeholderRes)
            }
            request.into(view)
        } else if (placeholderRes != 0) {
            view.setImageResource(placeholderRes)
        } else {
            view.setImageDrawable(null)
        }
    }
}